﻿using Client.Entity;
using Client.MainModule.Models;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.MainModule.ViewModels {
  public class TreeMenuViewModel {
    public List<MenuItemModel> Menus { get; set; } = new List<MenuItemModel>();
    //列表，没有树型结构
    private List<MenuEntity> origMenus = null;
    IRegionManager _regionManager = null;
    public TreeMenuViewModel(IRegionManager regionManager) {
      _regionManager = regionManager;
      //获取菜单数据
      origMenus = GlobalEntity.CurrentUserInfo?.Menus;
      FillMenus(Menus, 0);
    }
    private void FillMenus(List<MenuItemModel> menus,int parentId) {
      var sub=origMenus.Where(m=>m.ParentId == parentId).OrderBy(o => o.Index);
      if (sub.Count() > 0) {
        foreach(var item in sub) {
          MenuItemModel menuItemModel = new MenuItemModel(_regionManager) {
            MenuHeader = item.MenuHeader,
            MenuIcon = item.MenuIcon,
            TargetView = item.TargetView
          };
          menus.Add(menuItemModel);
          FillMenus(menuItemModel.Children = new List<MenuItemModel>(), item.MenuId);
        }
      }
    }

  }
}
